package com.apps.controller;

import com.apps.globals.sys.service.IKupaSysMessageService;
import com.apps.goods.service.IKupaGoodsCatService;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.helper.UserConextHelper;
import com.apps.member.account.model.po.KupaAccount;
import com.apps.member.account.service.IKupaAccountService;
import com.apps.member.message.service.IKupaMemberMessageCatService;
import com.apps.member.message.service.IKupaMemberMessageService;
import com.apps.member.service.IKupaMemberService;
import com.apps.sworld.sController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/page/member")
public class MemberController implements sController
{

    @Autowired
    private IKupaAccountService accountService;

    @Autowired
    private IKupaMemberMessageService memberMessageService;

    @Autowired
    private IKupaMemberService memberService;

    @Autowired
    private IKupaMemberMessageCatService memberMessageCatService;

    @Autowired
    private IKupaSysMessageService sysMessageService;

    @Autowired
    private IKupaGoodsCatService goodsCatService;

    /**
     * 用户基础信息
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping(value = "/info.html")
    public ModelAndView Info(ModelAndView modelAndView,
                             HttpServletRequest request)
    {
        modelAndView.setViewName("member/user_info");
        modelAndView.addObject("type",1);
        modelAndView.addObject("freecount",memberService.getTotalDownloadFreeCountByMemberId(UserConextHelper.getCurrentMember().getId()));
        //塞入系统通告
        modelAndView.addObject("sysmsg",sysMessageService.getOne());
        return modelAndView;
    }

    /**
     * 用户账户
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping(value = "/account.html")
    public ModelAndView Account(ModelAndView modelAndView,
                                HttpServletRequest request)
    {
        modelAndView.setViewName("member/user_account");
        modelAndView.addObject("type",2);
        //塞入账户数据
        modelAndView.addObject("account",accountService.getOneByMemberId(UserConextHelper.getCurrentMember().getId(),new KupaAccount()));
        //塞入系统通告
        modelAndView.addObject("sysmsg",sysMessageService.getOne());
        return modelAndView;
    }

    /**
     * 用户消息,cat=0查询所有消息
     * @param cat
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping(value = "/{cat}/{pageNo}/message.html")
    public ModelAndView Message(@PathVariable Integer cat,
                                @PathVariable Integer pageNo,
                                ModelAndView modelAndView,
                                HttpServletRequest request)
    {
        //每页数据量
        Integer pageSize=8;
        modelAndView.setViewName("member/user_msg");
        modelAndView.addObject("type",3);
        modelAndView.addObject("cat_id",cat);
        modelAndView.addObject("message",cat.intValue() == 0?memberMessageService.pageQuery(pageNo,pageSize):memberMessageService.pageQueryByCatId(cat,pageNo,pageSize));
        modelAndView.addObject("messagecat",memberMessageCatService.query());
        //塞入系统通告
        modelAndView.addObject("sysmsg",sysMessageService.getOne());
        return modelAndView;
    }


    /**
     * 用户的商品
     * @param type  查询类型 4=我的购买 5=我的下载 6=我的收藏 7=我的足迹(浏览)
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping(value = "/{type}/{cat_id}/{pageNo}/goods.html")
    public ModelAndView Goods(@PathVariable Integer type,
                              @PathVariable Integer cat_id,
                              @PathVariable Integer  pageNo,
                              ModelAndView modelAndView,
                              HttpServletRequest request)
    {
        //每页数据量
        Integer pageSize=8;

        modelAndView.setViewName("member/user_goods");
        switch (type.intValue())
        {
            case 4:
                modelAndView.addObject("goodslist",memberService.pageQueryGoodsByOwnBuys(UserConextHelper.getCurrentMember().getId(),cat_id,pageNo,pageSize));
                break;
            case 5:
                modelAndView.addObject("goodslist",memberService.pageQueryGoodsByDownload(UserConextHelper.getCurrentMember().getId(),cat_id,pageNo,pageSize));
                break;
            case 6:
                modelAndView.addObject("goodslist",memberService.pageQueryGoodsByCollect(UserConextHelper.getCurrentMember().getId(),cat_id,pageNo,pageSize));
                break;
            case 7:
                modelAndView.addObject("goodslist",memberService.pageQueryGoodsByHistoryBrows(UserConextHelper.getCurrentMember().getId(),cat_id,pageNo,pageSize));
                break;
        }
        modelAndView.addObject("type",type);
        //塞入系统通告
        modelAndView.addObject("sysmsg",sysMessageService.getOne());
        //塞入商品分类
        modelAndView.addObject("goods_cat_id",cat_id);
        return modelAndView;
    }

}
